package com.twu.biblioteca.controller;

import com.twu.biblioteca.domain.LibraryUser;
import com.twu.biblioteca.repository.LibraryEntityRepository;
import com.twu.biblioteca.repository.Repository;

import java.util.Collections;
import java.util.List;

public class LoginControllerImpl implements LoginController {

    private final Repository<LibraryUser> userRepository;
    private boolean isLoggedIn = false;
    private LibraryUser currentUser;

    public LoginControllerImpl(Repository<LibraryUser> userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public boolean login(String username, String password) {
        List<LibraryUser> users = this.userRepository.getAll();
        LibraryUser userToBeAuthenticated = new LibraryUser(username, password);
        int index = users.indexOf(userToBeAuthenticated);
        if(index != -1){
            isLoggedIn = true;
            currentUser = users.get(index);
            return true;
        }

        return false;
    }

    @Override
    public boolean getIsLoggedIn() {
        return isLoggedIn;
    }

    @Override
    public LibraryUser getCurrentUser() {
        return currentUser;
    }

    @Override
    public void printCurrentUser() {
        System.out.println(this.currentUser);
    }
}
